//
//  ViewController.m
//  14-CollectionViewTest
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "ClassifyCollectionViewCell.h"
#import "HotCollectionViewCell.h"
#import "LabelCollectionReusableView.h"
#import "ClassifyCollectionReusableView.h"
#define kWidth self.view.bounds.size.width
#define kHeight self.view.bounds.size.height
#define kArcRandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f  green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
#define kCollectionViewClassifyReuse @"classifyReuse"
#define kCollectionHotReuse @"hotReuse"
#define kCollectionHeaderReuse @"headerReuse"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *hotCollection;
@property (nonatomic, strong) UICollectionView *classifyCollection;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self createCollection];
    [self crearteHotCollection];
}
- (void)crearteHotCollection {
    // 创建布局
    UICollectionViewFlowLayout *hotLayout = [[UICollectionViewFlowLayout alloc] init];
    // 创建collection
    self.hotCollection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:hotLayout];
    // 设置item的大小
    hotLayout.itemSize = CGSizeMake((kWidth - 10) / 3, (kWidth - 10) / 3);
    // 设置行边距
    hotLayout.minimumLineSpacing = 5;
    // 设置列边距
    hotLayout.minimumInteritemSpacing = 0;
    // 设置头部视图大小
//    hotLayout.headerReferenceSize = CGSizeMake(0, 50);
    // 代理
    _hotCollection.delegate = self;
    _hotCollection.dataSource = self;
    // 背景色
    _hotCollection.backgroundColor = [UIColor whiteColor];
    // 注册cell
    [_hotCollection registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:kCollectionHotReuse];
    // 注册头部视图
    [_hotCollection registerClass:[LabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionHeaderReuse];
    [self.view addSubview:_hotCollection];
    /********** 1. 通过上边距实现视图添加自定义View(UICollectionView本质是UIScrollView) **************/
//    _hotCollection.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
//    UIView *viewOflabel = [[UIView alloc] initWithFrame:CGRectMake(0, -295, kWidth, 290)];
//    viewOflabel.backgroundColor = [UIColor redColor];
//    [_hotCollection addSubview:viewOflabel];
    //
    // 注册另一个头部视图
    [_hotCollection registerClass:[ClassifyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewClassifyReuse];
    
}
/**
 *  头部视图, 尾部视图的协议方法
 *
 *  @param collectionView collectionView调用此方法
 *  @param kind           UICollectionElementKindSectionHeader或者UICollectionElementKindSectionFooter
 *  @param indexPath      indexPath
 *
 *  @return UICollectionReusableView类型
 */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        ClassifyCollectionReusableView *classifyReuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewClassifyReuse forIndexPath:indexPath];
        return classifyReuseableView;
        
    } else
    {
        LabelCollectionReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionHeaderReuse forIndexPath:indexPath];
        ;
        return reuseableView;
    }
    
}
#pragma mark - 代理方法(dataSource)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 48;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
           HotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionHotReuse forIndexPath:indexPath];
        cell.contentView.backgroundColor = kArcRandomColor;
        return cell;
}

#pragma mark - 自定义item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
    } else {
        return CGSizeMake(0, 50);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
